//
//  CutomselfLikeAppstoreInstelself.m
//  Appstoreself
//
//  Created by ZhuoLaiQiang@Gmail.com on 11-11-26.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "CutomButtonLikeAppstoreInstelButton.h"
#import <QuartzCore/QuartzCore.h>

@interface CutomButtonLikeAppstoreInstelButton() 

- (void)selfIsTouchUpInSide;

@end

@implementation CutomButtonLikeAppstoreInstelButton

@synthesize  normalImage = _normalImage;
@synthesize  actionImage = _actionImage;

@synthesize  normalTitle = _normalTitle;
@synthesize  actionTitle = _actionTitle;

@synthesize delegate = _delegate;
@synthesize  actionSel;
@synthesize  addWidth = _addWidth;

@synthesize indexPahtForCell;


- (void)didMoveToSuperview
{
    [self addTarget: self action: @selector(selfIsTouchUpInSide) forControlEvents: UIControlEventTouchUpInside];
    CGRect frame = self.frame;
    self.frame = CGRectMake(frame.origin.x, frame.origin.y, _normalImage.size.width, _normalImage.size.height);
    [self.titleLabel setFont: [UIFont systemFontOfSize: 12]];
    [self setTitle: _normalTitle forState: UIControlStateNormal];
    self.backgroundColor = [UIColor colorWithPatternImage: _normalImage];
    
    self.layer.cornerRadius = 6;
    self.layer.masksToBounds = YES;
}

- (void)selfIsTouchUpInSide
{
    if (!self.selected)
    {
        self.backgroundColor = [UIColor colorWithPatternImage: _actionImage];
        [self setTitle:  nil forState: UIControlStateNormal];
        self.selected = YES;
        
        [UIView beginAnimations: nil context: nil];
        [UIView setAnimationDuration: 0.3];
        [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
        [UIView setAnimationDelegate: self];
        [UIView setAnimationDidStopSelector: @selector(animationIsEnd)];
        
        self.bounds = CGRectMake(self.bounds.origin.x, 
                                 self.bounds.origin.y, 
                                 _actionImage.size.width,
                                 self.bounds.size.height);
        
        self.layer.position = CGPointMake(self.layer.position.x - (_actionImage.size.width - _normalImage.size.width) / 2,
                                          self.layer.position.y);
        
        
        [UIView commitAnimations];
    }else
    {
        self.backgroundColor = [UIColor colorWithPatternImage: _normalImage];
        [self setTitle: nil forState: UIControlStateNormal];
        self.selected = NO;
        
        [UIView beginAnimations: nil context: nil];
        [UIView setAnimationDuration: 0.3];
        [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
        [UIView setAnimationDelegate: self];
        [UIView setAnimationDidStopSelector: @selector(animationIsEnd)];
        
        self.bounds = CGRectMake(self.bounds.origin.x, 
                                 self.bounds.origin.y, 
                                 _normalImage.size.width, 
                                 self.bounds.size.height);
        
        self.layer.position = CGPointMake(self.layer.position.x + (_actionImage.size.width - _normalImage.size.width) / 2, 
                                          self.layer.position.y);
        
        [UIView commitAnimations];
        
        if (_delegate && [_delegate respondsToSelector: actionSel]) 
        {
            [_delegate performSelector: actionSel withObject: self];
        }
    }
    
}

- (void)animationIsEnd
{
    if (self.selected) 
    {
        [self setTitle: _actionTitle forState: UIControlStateNormal];
    }else
    {
        [self setTitle: _normalTitle forState: UIControlStateNormal];
    }
}

- (void)dealloc
{
    [_normalImage release];
    [_actionTitle release];
    
    [_normalTitle release];
    [_actionTitle release];
    LF_RELEASE_SAFELY(indexPahtForCell);
    
    [super dealloc];
}

@end
